package com.jj.controller;

import com.jj.pojo.Emp;
import com.jj.pojo.Result;
import com.jj.service.EmpService;
import com.jj.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp  emp){
        log.info("登录信息:{}", emp);
        Emp e = empService.login(emp);
//        要是登录成功，返回登录信息，并下发令牌
        if (e != null){
//            自定义一个HashMap，用于存储jwt的自定义声明claims
            Map<String, Object> claims = new HashMap<>();
//            添加自定义声明
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());
//            调用工具类的这个方法生成jwt令牌
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }
}
